$(document).ready(function() {	
	initAddSelectedMenu();
	initCompanyRadio();
});



function initAddSelectedMenu(){
	var menuLayOut=$("#menuLayOut").val();
	var menuObj=evalJSON(menuLayOut);	
	var userCompanyLevel=menuObj.userCompanyLevel;	
	if(userCompanyLevel==PROVINCIAL_LEVEL){
		disableSelectMenu("#addProvincialSelectedMenu");
	}else if(userCompanyLevel==MUNIPITAL_LEVEL){
		disableSelectMenu("#addProvincialSelectedMenu");
		disableSelectMenu("#addMunicipalSelectedMenu");
	}
}

function initCompanyRadio(){
	var menuLayOut=$("#menuLayOut").val();
	var menuObj=evalJSON(menuLayOut);	
	var userCompanyLevel=menuObj.userCompanyLevel;	
	if(userCompanyLevel==PROVINCIAL_LEVEL){
		disableSelectMenu("#provincialradio");	
		$("#provincalTr").css("display","");		
	}else if(userCompanyLevel==MUNIPITAL_LEVEL){
		disableSelectMenu("#municipalradio");
		disableSelectMenu("#provincialradio");		
		$("#provincalTr").css("display","");
		$("#municipalTr").css("display","");
	}	
}

function getMunicipalAdd(){
	if ($('#addMunicipalSelectedMenu').is(':visible')){
	 var selectedProvincialCompany=$("#addProvincialSelectedMenu").val();
	 if(selectedProvincialCompany==0){
	   	clearDropDownSelectMenu('#addMunicipalSelectedMenu',CENTERDEAFAULTOPIONT);
		return;
	 }else{
		 addOptionByAjax('admin/ajaxGetMunicipalList.action',selectedProvincialCompany,'#addMunicipalSelectedMenu',MUNICIPALDEFAULTOPTION,"");
 	 }  
	}
}

function hideAllCompanySelectedMenu(){
	$("#provincalTr").css("display","none");
	$("#municipalTr").css("display","none");	
}

function showProvincial(){
	$("#provincalTr").css("display","");
	$("#municipalTr").css("display","none");
}

function showMunicipal(){
	$("#provincalTr").css("display","");
	$("#municipalTr").css("display","");
	var menuLayOut=$("#menuLayOut").val();
	var menuObj=evalJSON(menuLayOut);	
	var userCompanyLevel=menuObj.userCompanyLevel;
	//if user company level is municipal,don't work after click center button 
	if(userCompanyLevel!=3){
		getMunicipalAdd();
	}
}

function ableAllAddSelectMenu(){
	$("#addProvincialSelectedMenu").attr("disabled",false);
	$("#addMunicipalSelectedMenu").attr("disabled",false);
	
}

function saveCompany(){
	//ableAllAddSelectMenu();
	var parentCompanyId;
	var level;
	var radioValue = $('input[name=companyRadio][checked]').val();
	var provincialCompanyValue=$("#addProvincialSelectedMenu").val();
	var municipalCompanyValue=$("#addMunicipalSelectedMenu").val();
	
	//provincial
	if(radioValue==1){
		level=PROVINCIAL_LEVEL;
		//getuser company id
		var menuLayOut=$("#menuLayOut").val();
		var menuObj=evalJSON(menuLayOut);
		
		parentCompanyId=menuObj.userCompanyId;
	//municipal
	}else if(radioValue==2){
		level=MUNIPITAL_LEVEL;
		if(provincialCompanyValue==0){
			alert("请选择省级公司");
			return ;
		}else{
			parentCompanyId=provincialCompanyValue;
		}
	//center
	}else if(radioValue==3){
		level=CLAIM_CENTER_LEVEL;
	
		if(municipalCompanyValue==0){
			alert("请选择市级公司");
			return ;
		}else{
			parentCompanyId=municipalCompanyValue;
		}
	}
	sendAddCompanyRequest(parentCompanyId, level);
}

function sendAddCompanyRequest(parentCompanyId, level){
	var name = $("#company_companyName").val();
	var telephone = $("#company_telephone").val();
	var fax = $("#company_fax").val();
	var email = $("#company_email").val();
	var address = $("#company_address").val();
	var postCode = $("#company_postCode").val();;
	var remark = $("#company_remark").val();
	var params = {	"companyAddEdit.parentCompanyId":parentCompanyId,
			"companyAddEdit.companyLevel":level,
			"companyAddEdit.companyName":name,
			"companyAddEdit.telephone":telephone,
			"companyAddEdit.fax":fax,
			"companyAddEdit.email":email,
			"companyAddEdit.address":address,
			"companyAddEdit.postCode":postCode,
			"companyAddEdit.remark":remark}; 
	
	//check name
	var companyName=trim(name);	
		$.ajax({
			type:"POST",
			async:false,
			url:"admin/ajaxCheckCompanyName.action",
			data:{"companyName":companyName},
			success:function(data) {
				if(data>0){
					alert("单位名称已经存在,请修改单位名称");
					
				}else{
					if(checkCompanyInfo(name,telephone,fax,email,address,postCode,remark)){						
						$.ajax({
					        type:"POST",
					        async:false,
					        url:"admin/ajaxCompanyAdd.action",
					        data:params,
					        success:function(data2) {
								//set to div *********************************
								if(data2>0){
									//set value to forward to page which has same level with new company
									//-1 means selected all companys
									setValueToMenuLayOutForPaganiation('-1',parentCompanyId,level);															
									showAlertDialogue('添加成功，单位编码：'+data2,forwardToNewPage,'companyExcutePagination.action');
									//alert('showAlertDialogue end ');
									//forward to new page	
									}else{
										showAlertDialogue('添加失败');
									}
						    }
					    });
					}
				}
			  }
		});
}

function setValueToMenuLayOutForPaganiation(selectedCompanyId,parentCompanyId,level){
	var menuLayOut=$("#menuLayOut").val();
	var menuObj=evalJSON(menuLayOut);
	menuObj.currentSelectedCompanyId=selectedCompanyId;
	menuObj.parentCompanyId=parentCompanyId;
	menuObj.currentSelectedCompanyLevel=level;
	//set json string to hidden string
	var jsonStr=evalJSONString(menuObj);
	//clear page number
	$("#_p_currentPage_id").val("");
	$("#_p_totalPage_id").val("");	
	$("#menuLayOut").val(jsonStr);
}

function forwardToNewPage(url){
	$("#form1").attr("action",url);
    $("#form1").submit();
}



function checkCompanyInfo(name,telephone,fax,email,address,postCode,remark){
	
	return checkName(trim(name)) && 
			checkTel(trim(telephone)) &&
			checkFax(trim(fax)) &&
			checkEmail(trim(email)) &&
			checkAddress(trim(address)) &&
			checkPostCode(trim(postCode)) &&
			checkRemark(trim(remark));
}
